package org.sabayframework.activity;

import org.sabayframework.channel.Lock;
import org.sabayframework.channel.impl.ReadOnlyChannel;

public class GeneratorChannel extends ReadOnlyChannel<Integer> {

	private int blockSize;
	private volatile boolean closed;
	
	public GeneratorChannel(int blockSize) {
		super();
		this.blockSize = blockSize;
	}

	class ReadLock implements Lock<Integer>
	{
		int locked;
		
		ReadLock(int locked) {
			super();
			this.locked = locked;
		}

		@Override
		public int size() {
			return locked;
		}

		@Override
		public Integer get(int index) {
			return 0;
		}

		@Override
		public void set(int index, Integer elem) {
		}

		@Override
		public Integer[] toArray() {
			Integer[] a = new Integer[locked];
			for(int i=0;i<a.length;++i)a[i]=0;
			return a;
		}

		@Override
		public void unlock() {
		}

		@Override
		public void cancel() {
		}
		
	}
	@Override
	public Lock<Integer> getReadLock(int size) {
		return closed ? null:new ReadLock(size);
	}

	@Override
	public Integer remove() throws IllegalStateException {
		return 0;
	}

	@Override
	public boolean isEmpty() {
		return closed? true : false;
	}

	@Override
	public int size() {
		return closed ? 0 : blockSize;
	}

	@Override
	public void close() {
		closed = true;
	}

	@Override
	public boolean isClosed() {
		return closed;
	}

	@Override
	public Lock<Integer> waitForReadLock(int size, long timeout){
		return new ReadLock(size);
	}

}
